﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using FZKJ.DBUtility;
namespace FZKJ.DAL{
	 	//b_msgattachment
		public partial class b_msgattachment
	{
   		private readonly SQLHelper sqldal = null;
        public b_msgattachment(string connStr = "")
        {
            sqldal = new SQLHelper(connStr);
        }
   		
   		/// <summary>
        /// 是否存在该记录
        /// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from b_msgattachment");
			strSql.Append(" where ");
			                                       strSql.Append(" id = @id  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return sqldal.Exists(strSql.ToString(),parameters);
		}
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(FZKJ.Model.b_msgattachment model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into b_msgattachment(");			
            strSql.Append("msgcode,name,filename,downtimes,isenable,sort,createtime,createman,isdelete,deletetime,deleteman");
			strSql.Append(") values (");
            strSql.Append("@msgcode,@name,@filename,@downtimes,@isenable,@sort,@createtime,@createman,@isdelete,@deletetime,@deleteman");            
            strSql.Append(") ");      
            strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
			            new SqlParameter("@msgcode", model.msgcode) ,            
                        new SqlParameter("@name", model.name) ,            
                        new SqlParameter("@filename", model.filename) ,            
                        new SqlParameter("@downtimes", model.downtimes) ,            
                        new SqlParameter("@isenable", model.isenable) ,            
                        new SqlParameter("@sort", model.sort) ,            
                        new SqlParameter("@createtime", model.createtime) ,            
                        new SqlParameter("@createman", model.createman) ,            
                        new SqlParameter("@isdelete", model.isdelete) ,            
                        new SqlParameter("@deletetime", model.deletetime) ,            
                        new SqlParameter("@deleteman", model.deleteman)             
              
            };
			object obj = sqldal.GetSingle(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			else
			{                  
            	return Convert.ToInt32(obj);    
			}
		}		
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public int Update(FZKJ.Model.b_msgattachment model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update b_msgattachment set ");
			                                                
            strSql.Append(" msgcode = @msgcode , ");                                    
            strSql.Append(" name = @name , ");                                    
            strSql.Append(" filename = @filename , ");                                    
            strSql.Append(" downtimes = @downtimes , ");                                    
            strSql.Append(" isenable = @isenable , ");                                    
            strSql.Append(" sort = @sort , ");                                    
            strSql.Append(" createtime = @createtime , ");                                    
            strSql.Append(" createman = @createman , ");                                    
            strSql.Append(" isdelete = @isdelete , ");                                    
            strSql.Append(" deletetime = @deletetime , ");                                    
            strSql.Append(" deleteman = @deleteman  ");            			
			strSql.Append(" where id=@id ");
						
			SqlParameter[] parameters = {
			            new SqlParameter("@id", model.id ) ,            
                        new SqlParameter("@msgcode", model.msgcode ) ,            
                        new SqlParameter("@name", model.name ) ,            
                        new SqlParameter("@filename", model.filename ) ,            
                        new SqlParameter("@downtimes", model.downtimes ) ,            
                        new SqlParameter("@isenable", model.isenable ) ,            
                        new SqlParameter("@sort", model.sort ) ,            
                        new SqlParameter("@createtime", model.createtime ) ,            
                        new SqlParameter("@createman", model.createman ) ,            
                        new SqlParameter("@isdelete", model.isdelete ) ,            
                        new SqlParameter("@deletetime", model.deletetime ) ,            
                        new SqlParameter("@deleteman", model.deleteman )             
              
            };
            
            return sqldal.ExecuteSql(strSql.ToString(),parameters);
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from b_msgattachment ");
			strSql.Append(" where id=@id");
						SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;


			int rows=sqldal.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from b_msgattachment ");
			strSql.Append(" where ID in ("+idlist + ")  ");
			int rows=sqldal.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public FZKJ.Model.b_msgattachment GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id, msgcode, name, filename, downtimes, isenable, sort, createtime, createman, isdelete, deletetime, deleteman  ");			
			strSql.Append("  from b_msgattachment ");
			strSql.Append(" where id=@id");
						SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			
			FZKJ.Model.b_msgattachment model=new FZKJ.Model.b_msgattachment();
			DataSet ds=sqldal.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
												if(ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
																																				model.msgcode= ds.Tables[0].Rows[0]["msgcode"].ToString();
																																model.name= ds.Tables[0].Rows[0]["name"].ToString();
																																model.filename= ds.Tables[0].Rows[0]["filename"].ToString();
																												if(ds.Tables[0].Rows[0]["downtimes"].ToString()!="")
				{
					model.downtimes=int.Parse(ds.Tables[0].Rows[0]["downtimes"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["isenable"].ToString()!="")
				{
					model.isenable=int.Parse(ds.Tables[0].Rows[0]["isenable"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["sort"].ToString()!="")
				{
					model.sort=int.Parse(ds.Tables[0].Rows[0]["sort"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["createtime"].ToString()!="")
				{
					model.createtime=DateTime.Parse(ds.Tables[0].Rows[0]["createtime"].ToString());
				}
																																				model.createman= ds.Tables[0].Rows[0]["createman"].ToString();
																												if(ds.Tables[0].Rows[0]["isdelete"].ToString()!="")
				{
					model.isdelete=int.Parse(ds.Tables[0].Rows[0]["isdelete"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["deletetime"].ToString()!="")
				{
					model.deletetime=DateTime.Parse(ds.Tables[0].Rows[0]["deletetime"].ToString());
				}
																																				model.deleteman= ds.Tables[0].Rows[0]["deleteman"].ToString();
																										
				return model;
			}
			else
			{
				return null;
			}
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataTable GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM b_msgattachment ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return sqldal.Query(strSql.ToString()).Tables[0];
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataTable GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM b_msgattachment ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return sqldal.Query(strSql.ToString()).Tables[0];
		}


		/// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM b_msgattachment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = sqldal.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataTable GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from b_msgattachment T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);           
            return sqldal.Query(strSql.ToString()).Tables[0];
        }
	}
}